<template>
  <el-card v-loading="loading">
    <div slot="header" class="clearfix">
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('save') }}</el-button>
      <el-button @click="closeTab()">{{ $t('close') }}</el-button>
    </div>
    <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="'160px'">
      <span>接口基本信息</span>
      <el-row style="margin-top: 10px;">
        <el-col :span="8">
          <!-- 接口编码 -->
          <el-form-item label="接口编码"
                        prop="apiCode">
            <!--文本框-->
            <el-input v-model="dataForm.apiCode"
                      placeholder="接口编码" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <!-- 接口名称 -->
          <el-form-item label="接口名称"
                        prop="apiName">
            <!--文本框-->
            <el-input v-model="dataForm.apiName"
                      placeholder="接口名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <!-- 接口处理方式 -->
          <el-form-item label="异常通知"
                        prop="msgPush">
            <el-checkbox v-model="dataForm.msgPush"></el-checkbox>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <!-- 接口数据提供者 -->
          <el-form-item label="数据提供者"
                        prop="apiProvider">
            <!--文本框-->
            <!--下拉框-->
            <el-select style="width: 100%;" @change="requestFlagChange" v-model="dataForm.apiProvider" placeholder="数据提供者" clearable>
              <el-option v-for="template in appList" :key="template.appId"
                         :label="template.appName" :value="template.appId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <!-- 接口数据使用者 -->
          <el-form-item label="数据使用者"
                        prop="apiConsumer">
            <!--文本框-->
            <el-select style="width: 100%;" @change="requestFlagChange" v-model="dataForm.apiConsumer" placeholder="数据使用者" clearable>
              <el-option v-for="template in appList" :key="template.appId"
                         :label="template.appName" :value="template.appId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <!-- 接口调用方式 -->
          <el-form-item label="调用方式"
                        prop="apiRequestType">
            <!--文本框-->
            <el-select style="width: 100%;" @change="requestFlagChange" v-model="dataForm.apiRequestType" placeholder="调用方式" clearable>
              <el-option v-for="template in $getDictList('API_REQUEST_TYPE', true)" :key="template.val"
                         :label="template.label" :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <!-- 接口类型 -->
          <el-form-item label="接口类型"
                        prop="apiType">
            <!--文本框-->
            <el-select style="width: 100%;" v-model="dataForm.apiType" placeholder="接口类型" clearable>
              <el-option v-for="template in $getDictList('API_TYPE', true)" :key="template.val"
                         :label="template.label" :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <!-- 接口通信协议 -->
          <el-form-item label="通信协议"
                        prop="apiProtocolType">
            <!--文本框-->
            <el-select style="width: 100%;" v-model="dataForm.apiProtocolType" placeholder="通信协议" clearable>
              <el-option v-for="template in $getDictList('API_PROTOCOL_TYPE', true)" :key="template.val"
                         :label="template.label" :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <!-- 接口处理方式 -->
          <el-form-item label="处理方式"
                        prop="apiDealType">
            <el-select style="width: 100%;" @change="requestFlagChange" v-model="dataForm.apiDealType" placeholder="处理方式" clearable>
              <el-option v-for="template in $getDictList('API_DEAL_TYPE', true)" :key="template.val"
                         :label="template.label" :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider></el-divider>
      <span>接口拓展信息</span>
      <el-row style="margin-top: 10px;">
        <el-col :span="12" v-if="requestFlag==='1'">
          <!-- 接口URL -->
          <el-form-item label="接收数据处理Java类" prop="receiptStepClass">
            <!--文本框-->
            <el-input v-model="dataForm.receiptStepClass" placeholder="接收数据处理类" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="requestFlag==='1'">
          <!-- 接口URL -->
          <el-form-item label="接收数据处理Java方法" prop="receiptStepMethod">
            <!--文本框-->
            <el-input v-model="dataForm.receiptStepMethod" placeholder="接收数据处理方法" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="requestFlag==='3'">
          <!-- 接口URL -->
          <el-form-item label="查询接口数据Java类" prop="queryStepClass">
            <!--文本框-->
            <el-input v-model="dataForm.queryStepClass" placeholder="查询接口数据Java类" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="requestFlag==='3'">
          <!-- 接口URL -->
          <el-form-item label="查询接口数据Java方法" prop="queryStepMethod">
            <!--文本框-->
            <el-input v-model="dataForm.queryStepMethod" placeholder="查询接口数据Java方法" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="requestFlag==='2'||requestFlag==='3'">
          <!-- 接口URL -->
          <el-form-item label="调用接口Java类" prop="responseStepClass">
            <!--文本框-->
            <el-input v-model="dataForm.responseStepClass" placeholder="调用接口Java类" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="requestFlag==='2'||requestFlag==='3'">
          <!-- 接口URL -->
          <el-form-item label="调用接口Java方法" prop="responseStepMethod">
            <!--文本框-->
            <el-input v-model="dataForm.responseStepMethod" placeholder="调用接口Java方法" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="requestFlag==='3'">
          <!-- 接口URL -->
          <el-form-item label="更新调用状态Java类" prop="updateStepClass">
            <!--文本框-->
            <el-input v-model="dataForm.updateStepClass" placeholder="更新调用状态Java类" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="requestFlag==='3'">
          <!-- 接口URL -->
          <el-form-item label="更新调用状态Java方法" prop="updateStepMethod">
            <!--文本框-->
            <el-input v-model="dataForm.updateStepMethod" placeholder="更新调用状态Java方法" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="requestFlag==='2'||requestFlag==='3'">
          <!-- 接口URL -->
          <el-form-item label="接口URL" prop="apiUrl">
            <!--文本框-->
            <el-input v-model="dataForm.apiUrl" placeholder="接口URL" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 接口访问扩展参数json -->
          <el-form-item label="扩展参数" prop="extendJson">
            <!--文本域-->
            <el-input type="textarea" :rows="3" v-model="dataForm.extendJson" placeholder="扩展参数"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 接口访问扩展参数json -->
          <el-form-item label="接口描述" prop="apiRemark">
            <!--文本域-->
            <el-input type="textarea" :rows="2" v-model="dataForm.apiRemark" placeholder="接口描述"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
</template>

<script>
import debounce from 'lodash/debounce'
import mainContent from '@/views/main-content'
var content = mainContent

export default {
  data () {
    return {
      timestamp: this.$route.params.timestamp,
      loading: false,
      requestFlag: '1', // 1-调入接口 2-同步调出接口 3-异步调出接口
      appList: [],
      dataForm: {
        id: this.$route.params.id,
        apiCode: '',
        apiName: '',
        apiProvider: '',
        apiConsumer: '',
        apiRequestType: '',
        apiType: '',
        apiProtocolType: '',
        apiDealType: '',
        receiptStepClass: '',
        receiptStepMethod: '',
        queryStepClass: '',
        queryStepMethod: '',
        responseStepClass: '',
        responseStepMethod: '',
        updateStepClass: '',
        updateStepMethod: '',
        apiUrl: '',
        extendJson: '',
        apiRemark: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        apiCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        apiName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        apiProvider: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        apiConsumer: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        apiRequestType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        apiType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        apiProtocolType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        apiDealType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        receiptStepClass: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        receiptStepMethod: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        queryStepClass: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        queryStepMethod: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        responseStepClass: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        responseStepMethod: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateStepClass: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateStepMethod: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    // 通过路由参数pid, 控制列表请求操作
    this.$nextTick(() => {
      this.$refs['dataForm'].resetFields()
      this.getAppInfo()
      if (this.$route.params.id) {
        this.dataForm.id = this.$route.params.id
        this.loading = true
        this.getInfo()
      }
    })
  },
  methods: {
    // 获取信息
    // 获取信息
    getInfo () {
      this.$http.get(`/api/apiConf/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        this.requestFlagChange()
      }).catch(() => {})
    },
    getAppInfo () {
      this.$http.get(`/sys/sysApp/listAll`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.appList = res.data
      }).catch(() => {})
    },
    requestFlagChange () {
      if (this.dataForm.apiProvider === 'self' && this.dataForm.apiRequestType === 'pull') {
        this.requestFlag = '1'
      } else if (this.dataForm.apiConsumer === 'self' && this.dataForm.apiRequestType === 'push') {
        this.requestFlag = '1'
      } else if (this.dataForm.apiProvider === 'self' && this.dataForm.apiRequestType === 'push' && this.dataForm.apiDealType === 'SYNC') {
        this.requestFlag = '2'
      } else if (this.dataForm.apiConsumer === 'self' && this.dataForm.apiRequestType === 'pull' && this.dataForm.apiDealType === 'SYNC') {
        this.requestFlag = '2'
      } else if (this.dataForm.apiProvider === 'self' && this.dataForm.apiRequestType === 'push' && this.dataForm.apiDealType === 'ASYNC') {
        this.requestFlag = '3'
      } else if (this.dataForm.apiConsumer === 'self' && this.dataForm.apiRequestType === 'pull' && this.dataForm.apiDealType === 'ASYNC') {
        this.requestFlag = '3'
      }
    },
    // 关闭 tab
    closeTab () {
      content.methods.tabRemoveHandle(this.$route.name)
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/api/apiConf/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.closeTab()
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
